package com.example.houseitemmanagebackend.controller;

import com.example.houseitemmanagebackend.entity.Locations;
import com.example.houseitemmanagebackend.security.JwtUserDetails;
import com.example.houseitemmanagebackend.service.LocationService;
import com.example.houseitemmanagebackend.service.UserLocationService;
import com.example.houseitemmanagebackend.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 位置管理控制器
 */
@Tag(name = "位置管理", description = "物品位置管理相关接口")
@RestController
@RequestMapping("/locations")
public class LocationController {

    @Autowired
    private LocationService locationService;

    @Autowired
    private UserLocationService userLocationService;

    @Operation(summary = "获取用户位置列表", description = "获取用户位置列表接口")
    @GetMapping("/{id}")
    public Result<List<Locations>> getLocationList(@AuthenticationPrincipal JwtUserDetails userDetails) {
        return Result.success(locationService.getLocationList(userDetails.getId()));
    }

    @Operation(summary = "添加位置", description = "添加位置接口")
    @PostMapping
    public Result<Map<String, Integer>> addLocation(@RequestBody Map<String, String> params,
            @AuthenticationPrincipal JwtUserDetails userDetails) {
        String name = params.get("name");
        if (name == null || name.trim().isEmpty()) {
            return Result.error(400, "位置名称不能为空");
        }

        Integer locationId = locationService.addLocation(name, userDetails.getId());

        // 将用户设置为位置管理员（roleType=1表示管理员）
        userLocationService.addUserToLocation(userDetails.getId(), locationId, 1);

        return Result.success(Map.of("id", locationId));
    }

    @Operation(summary = "为指定用户创建新位置", description = "为指定用户创建一个新的位置")
    @PostMapping("/{userId}")
    public Result<Map<String, Integer>> createLocationForUser(
            @PathVariable Integer userId,
            @RequestBody Map<String, String> params) {
        String name = params.get("name");
        if (name == null || name.trim().isEmpty()) {
            return Result.error(400, "位置名称不能为空");
        }

        Integer locationId = locationService.addLocation(name, userId);

        // 将用户设置为位置管理员（roleType=1表示管理员）
        userLocationService.addUserToLocation(userId, locationId, 1);

        return Result.success(Map.of("id", locationId));
    }

    @Operation(summary = "更新位置", description = "更新位置接口")
    @PutMapping("/{id}")
    public Result<Void> updateLocation(@PathVariable Integer id,
            @RequestBody Map<String, String> params,
            @AuthenticationPrincipal JwtUserDetails userDetails) {
        String name = params.get("name");
        if (name == null || name.trim().isEmpty()) {
            return Result.error(400, "位置名称不能为空");
        }

        locationService.updateLocation(id, name, userDetails.getId());
        return Result.success();
    }

    @Operation(summary = "删除位置", description = "删除位置接口")
    @DeleteMapping("/{id}")
    public Result<Void> deleteLocation(@PathVariable Integer id,
            @AuthenticationPrincipal JwtUserDetails userDetails) {
        locationService.deleteLocation(id, userDetails.getId());
        return Result.success();
    }
}
